# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.html

import scrapy

from components.custom_item import UpdateCustomItem
from gne import GeneralNewsExtractor


class ListedCompanyItem(UpdateCustomItem):
    # __update_key__ = ['company_full_name', ]
    __update_key__ = []
    __table_name__ = 'net_listed_company_info'

    company_full_name = scrapy.Field()
    company_eng_name = scrapy.Field()
    stock_code = scrapy.Field()
    stock_abb = scrapy.Field()
    expand_stock_abb = scrapy.Field()
    stock_sector = scrapy.Field()
    stock_state = scrapy.Field()
    legal_name = scrapy.Field()
    board_secretary_name = scrapy.Field()
    registered_address = scrapy.Field()
    cor_address = scrapy.Field()
    zip_code = scrapy.Field()
    email = scrapy.Field()
    list_date = scrapy.Field()
    total_share_capital = scrapy.Field()
    limited_share_capital = scrapy.Field()
    out_share_capital = scrapy.Field()
    telphone = scrapy.Field()
    website = scrapy.Field()
    listed_exchange = scrapy.Field()
    source_url = scrapy.Field()
    delisting_date = scrapy.Field()
    security_type = scrapy.Field()
    whet_not_st_company = scrapy.Field()
    whet_not_delisting_organize_stocks = scrapy.Field()
    province = scrapy.Field()
    taxpayer_industry1 = scrapy.Field()
    taxpayer_industry2 = scrapy.Field()
    trade_type = scrapy.Field()
    data_newest_query_date = scrapy.Field()


class DelistingInformationSourceItem(UpdateCustomItem):
    # __update_key__ = ['company_full_name', ]
    __update_key__ = []
    __table_name__ = 'net_delisting_information_source'

    company_full_name = scrapy.Field()
    company_eng_name = scrapy.Field()
    stock_code = scrapy.Field()
    stock_abb = scrapy.Field()
    expand_stock_abb = scrapy.Field()
    stock_sector = scrapy.Field()
    stock_state = scrapy.Field()
    legal_name = scrapy.Field()
    board_secretary_name = scrapy.Field()
    registered_address = scrapy.Field()
    cor_address = scrapy.Field()
    zip_code = scrapy.Field()
    email = scrapy.Field()
    list_date = scrapy.Field()
    total_share_capital = scrapy.Field()
    limited_share_capital = scrapy.Field()
    out_share_capital = scrapy.Field()
    telphone = scrapy.Field()
    website = scrapy.Field()
    listed_exchange = scrapy.Field()
    source_url = scrapy.Field()
    delisting_date = scrapy.Field()
    security_type = scrapy.Field()
    whet_not_st_company = scrapy.Field()
    whet_not_delisting_organize_stocks = scrapy.Field()
    province = scrapy.Field()
    taxpayer_industry1 = scrapy.Field()
    taxpayer_industry2 = scrapy.Field()
    trade_type = scrapy.Field()
    data_newest_query_date = scrapy.Field()


class NetListedCompanyYearReportItem(UpdateCustomItem):
    # __update_key__ = ['company_full_name', ]
    __update_key__ = []
    __table_name__ = 'net_listed_company_year_report'

    stock_code = scrapy.Field()
    stock_abb = scrapy.Field()
    report_uuid = scrapy.Field()
    report_name = scrapy.Field()
    report_url = scrapy.Field()
    report_date = scrapy.Field()
    report_file_data = scrapy.Field()


class NetListedCompanyIndustryItem(UpdateCustomItem):
    # __update_key__ = ['company_full_name', ]
    __update_key__ = []
    __table_name__ = 'net_listed_company_industry'

    stock_code = scrapy.Field()
    stock_abb = scrapy.Field()
    industry1 = scrapy.Field()
    industry2 = scrapy.Field()


class NetListedCompanyValuationDataDetailsItem(UpdateCustomItem):
    # __update_key__ = ['company_full_name', ]
    __update_key__ = []
    __table_name__ = 'net_listed_company_valuation_data_details'

    stock_code = scrapy.Field()
    stock_abb = scrapy.Field()
    time = scrapy.Field()
    daily_closing_price = scrapy.Field()
    daily_increase_or_decrease = scrapy.Field()
    total_market_value = scrapy.Field()
    circulation_market_value = scrapy.Field()
    total_share_capital = scrapy.Field()
    out_share_capital = scrapy.Field()
    pettm = scrapy.Field()
    static_pe = scrapy.Field()
    pb = scrapy.Field()
    peg = scrapy.Field()
    pcf = scrapy.Field()
    ps = scrapy.Field()


class NetAdministrativePenaltiesItem(UpdateCustomItem):
    # __update_key__ = ['company_full_name', ]
    __update_key__ = []
    __table_name__ = 'net_administrative_penalties'

    title = scrapy.Field()
    decision_no = scrapy.Field()
    published_date = scrapy.Field()
    content = scrapy.Field()
    decision_uuid = scrapy.Field()
    url = scrapy.Field()


class NetMoneySupply(UpdateCustomItem):
    # __update_key__ = ['company_full_name', ]
    __update_key__ = []
    __table_name__ = 'net_money_supply'

    time = scrapy.Field()
    m2_supply_final_value = scrapy.Field()
    m2_yoy_increase = scrapy.Field()
    m1_supply_final_value = scrapy.Field()
    m1_yoy_increase = scrapy.Field()
    m0_supply_final_value = scrapy.Field()
    m0_yoy_increase = scrapy.Field()


class NetCreditFundsSummary(UpdateCustomItem):
    # __update_key__ = ['company_full_name', ]
    __update_key__ = []
    __table_name__ = 'net_credit_funds_summary'

    time = scrapy.Field()
    total_deposits = scrapy.Field()
    domestic_deposits = scrapy.Field()
    deposits_of_households = scrapy.Field()
    households_demand_deposits = scrapy.Field()
    households_time_other_deposits = scrapy.Field()
    deposits_of_non_financial_enterprises = scrapy.Field()
    non_financial_enterprises_demand_deposits = scrapy.Field()
    non_financial_enterprises_time_other_deposits = scrapy.Field()
    deposits_of_government_departments_organizations = scrapy.Field()
    fiscal_deposits = scrapy.Field()
    deposits_of_non_banking_financial_institutions = scrapy.Field()
    overseas_deposits = scrapy.Field()
    financial_bonds = scrapy.Field()
    currency_in_circulation = scrapy.Field()
    liabilities_to_international_financial_institutions = scrapy.Field()
    short_term = scrapy.Field()
    total_funds_sources = scrapy.Field()
    total_loans = scrapy.Field()
    domestic_loans = scrapy.Field()
    loans_to_households = scrapy.Field()
    households_short_term_loans = scrapy.Field()
    households_short_term_consumption_loans = scrapy.Field()
    households_short_term_operating_loans = scrapy.Field()
    households_mid_long_term_loans = scrapy.Field()
    households_mid_long_term_consumption_loans = scrapy.Field()
    households_mid_long_term_operating_loans = scrapy.Field()
    loans_to_enterprises_gov = scrapy.Field()
    short_term_loans_enterprises_gov = scrapy.Field()
    mid_long_term_loans_enterprises_gov = scrapy.Field()
    paper_financing_enterprises_gov = scrapy.Field()
    financial_leases_enterprises_gov = scrapy.Field()
    total_advances_enterprises_gov = scrapy.Field()
    loans_to_non_banking_financial_organizations = scrapy.Field()
    overseas_loans = scrapy.Field()
    portfolio_investments = scrapy.Field()
    shares_and_other_investments = scrapy.Field()
    position_for_bullion_purchase = scrapy.Field()
    foreign_exchange = scrapy.Field()
    assets_with_international_financial_institutions = scrapy.Field()
    total_funds_uses = scrapy.Field()


class NetNewRmbLoans(UpdateCustomItem):
    # __update_key__ = ['company_full_name', ]
    __update_key__ = []
    __table_name__ = 'net_new_rmb_loans'

    time = scrapy.Field()
    new_rmb_loans_total = scrapy.Field()
    new_rmb_loans_yoy = scrapy.Field()
    new_rmb_loans_mom = scrapy.Field()
    accumulated_rmb_loans_total = scrapy.Field()
    accumulated_rmb_loans_yoy = scrapy.Field()


class NetAggregateFinancingFlow(UpdateCustomItem):
    # __update_key__ = ['company_full_name', ]
    __update_key__ = []
    __table_name__ = 'net_aggregate_financing_flow'

    time = scrapy.Field()
    afre_flow = scrapy.Field()
    rmb_loans = scrapy.Field()
    foreign_currency_denominated_loans = scrapy.Field()
    entrusted_loans = scrapy.Field()
    trust_loans = scrapy.Field()
    undiscounted_bankers_acceptances = scrapy.Field()
    net_financing_of_corporate_bonds = scrapy.Field()
    government_bonds = scrapy.Field()
    equity_financing_by_non_financial_enterprises = scrapy.Field()
    asset_backed_securities_by_depository_institutions = scrapy.Field()
    loans_written_off = scrapy.Field()


class NetAggregateFinancingStock(UpdateCustomItem):
    # __update_key__ = ['company_full_name', ]
    __update_key__ = []
    __table_name__ = 'net_aggregate_financing_stock'

    time = scrapy.Field()
    type = scrapy.Field()
    afre_stock = scrapy.Field()
    rmb_loans = scrapy.Field()
    foreign_currency_denominated_loans = scrapy.Field()
    entrusted_loans = scrapy.Field()
    trust_loans = scrapy.Field()
    undiscounted_bankers_acceptances = scrapy.Field()
    net_financing_of_corporate_bonds = scrapy.Field()
    government_bonds = scrapy.Field()
    equity_financing_by_non_financial_enterprises = scrapy.Field()
    asset_backed_securities_by_depository_institutions = scrapy.Field()
    loans_written_off = scrapy.Field()


class NetAggregateFinancingFlowByProvince(UpdateCustomItem):
    # __update_key__ = ['company_full_name', ]
    __update_key__ = []
    __table_name__ = 'net_aggregate_financing_flow_by_province'

    time = scrapy.Field()
    province = scrapy.Field()
    afre_flow = scrapy.Field()
    rmb_loans = scrapy.Field()
    foreign_currency_denominated_loans = scrapy.Field()
    entrusted_loans = scrapy.Field()
    trust_loans = scrapy.Field()
    undiscounted_bankers_acceptances = scrapy.Field()
    net_financing_of_corporate_bonds = scrapy.Field()
    government_bonds = scrapy.Field()
    equity_financing_by_non_financial_enterprises = scrapy.Field()


class NetStandardizedExpressionOfBusinessScopeData(UpdateCustomItem):
    # __update_key__ = ['company_full_name', ]
    __update_key__ = []
    __table_name__ = 'net_standardized_expression_of_business_scope_data'

    business_scope_name = scrapy.Field()
    province = scrapy.Field()
    entry_code = scrapy.Field()
    industry_name = scrapy.Field()
    entry_status = scrapy.Field()
    license_situation = scrapy.Field()
    range_describe = scrapy.Field()
    search_results = scrapy.Field()
    related_activities = scrapy.Field()
    non_related_activities = scrapy.Field()
    notes = scrapy.Field()


class NetRelatedMattersData(UpdateCustomItem):
    # __update_key__ = ['company_full_name', ]
    __update_key__ = []
    __table_name__ = 'net_related_matters_data'

    business_scope_name = scrapy.Field()
    province = scrapy.Field()
    related_matters_data = scrapy.Field()
    competent_department = scrapy.Field()
    approval_levels_and_departments = scrapy.Field()
    use_business_phase = scrapy.Field()
    reform_methods = scrapy.Field()
    applicable_area = scrapy.Field()


class NetRelevantPolicyAndRegulatoryData(UpdateCustomItem):
    # __update_key__ = ['company_full_name', ]
    __update_key__ = []
    __table_name__ = 'net_relevant_policy_and_regulatory_data'

    business_scope_name = scrapy.Field()
    province = scrapy.Field()
    names_of_relevant_policies_and_regulations = scrapy.Field()
    doc_number = scrapy.Field()
    state = scrapy.Field()
    applicable_area = scrapy.Field()
    release_time = scrapy.Field()
    abstract = scrapy.Field()
    law_id = scrapy.Field()


class NetEnvironmentalImpactAssessmentPromptData (UpdateCustomItem):
    # __update_key__ = ['company_full_name', ]
    __update_key__ = []
    __table_name__ = 'net_environmental_impact_assessment_prompt_data'

    business_scope_name = scrapy.Field()
    province = scrapy.Field()
    environmental_impact_assessment_prompt_name = scrapy.Field()
    all_report_levels_involved = scrapy.Field()
    a_registration_form = scrapy.Field()
    first_level_classification_id = scrapy.Field()
    environmental_sensitive_area = scrapy.Field()
    current_code_this_data_in_the_directory_number_of_occurrences = scrapy.Field()
    the_highest_level_of_reporting_involved = scrapy.Field()
    report_book = scrapy.Field()
    second_level_classification_directory_name = scrapy.Field()


class NetTaxBureauPoliciesAndRegulations(UpdateCustomItem):
    # __update_key__ = ['company_full_name', ]
    __update_key__ = []
    __table_name__ = 'net_tax_bureau_policies_and_regulations'

    pid = scrapy.Field()
    type = scrapy.Field()
    title = scrapy.Field()
    document_number = scrapy.Field()
    written_date = scrapy.Field()
    explanatory_note = scrapy.Field()
    link = scrapy.Field()
    text = scrapy.Field()
    department_of_publication = scrapy.Field()


class NewsAndUpdatesFromLocalTaxBureaus(UpdateCustomItem):
    # __update_key__ = ['company_full_name', ]
    __update_key__ = []
    __table_name__ = 'news_and_updates_from_local_tax_bureaus'

    pid = scrapy.Field()
    province = scrapy.Field()
    title = scrapy.Field()
    content_type = scrapy.Field()
    document_number = scrapy.Field()
    text = scrapy.Field()
    department_of_publication = scrapy.Field()
    link = scrapy.Field()
    release_time = scrapy.Field()

    def pre_to_db(self):
        extractor = GeneralNewsExtractor()
        result = self.text
        self.text = {"text": extractor.extract(result['text'])['content']}


class DishonestPersonSubjectToEnforcement(UpdateCustomItem):
    # __update_key__ = ['company_full_name', ]
    __update_key__ = []
    __table_name__ = 'dishonest_person_subject_to_enforcement'

    name = scrapy.Field()
    cardnum = scrapy.Field()
    court_of_execution = scrapy.Field()
    province = scrapy.Field()
    reference = scrapy.Field()
    obligations_determined_by_effective_legal_documents = scrapy.Field()
    performance_of_the_executed_party = scrapy.Field()
    specific_situation = scrapy.Field()
    release_time = scrapy.Field()
